package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import bean.Bean;
import dao.ClientDAO;
import dao.GoodsDAO;

public class ClientServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html; charset=utf-8");
		request.setCharacterEncoding("utf-8");
		HttpSession session = request.getSession();
		PrintWriter out = response.getWriter();

		Bean bean = new Bean();
		ClientDAO dao = new ClientDAO();

		int kind = Integer.parseInt(request.getParameter("kind"));
		if (kind == 1) {
			String name = request.getParameter("name");
			String phone = request.getParameter("phone");
			String mail = request.getParameter("mail");
			String description = request.getParameter("description");
			bean.setName(name);
			bean.setPhone(phone);
			bean.setMail(mail);
			bean.setDescription(description);
			int i = dao.add(bean);
			if (i > 0) {
				out.print("<script>alert('操作成功！');location.href='admin/client_add.jsp';</script>");
			} else {
				out.print("<script>alert('操作失败！');history.go(-1);</script>");
			}
		} else if (kind == 2) {
			int id = Integer.parseInt(request.getParameter("id"));
			String name = request.getParameter("name");
			String phone = request.getParameter("phone");
			String description = request.getParameter("description");
			bean.setName(name);
			bean.setPhone(phone);
			bean.setDecision(description);
			int i = dao.update(bean, id);
			if (i > 0) {
				out.print("<script>alert('操作成功！');location.href='admin/client_edit.jsp?id="
						+ id + "';</script>");
			} else {
				out.print("<script>alert('操作失败！');history.go(-1);</script>");
			}
		} else if (kind == 3) {
			int id = Integer.parseInt(request.getParameter("id"));
			int i = dao.delete(id);
			if (i > 0) {
				out.print("<script>alert('操作成功！');location.href='admin/client_list.jsp';</script>");
			} else {
				out.print("<script>alert('操作失败！');history.go(-1);</script>");
			}
		}else if (kind == 4) {
			int uid = Integer.parseInt(request.getParameter("id"));
			int pid = Integer.parseInt(request.getParameter("goodsid"));
			SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String time = sd.format(new Date());
			bean.setUid(uid);
			bean.setPid(pid);
			bean.setTime(time);
			int i = dao.rent(bean);
			if (i > 0) {
				GoodsDAO goodsdao = new GoodsDAO();
				goodsdao.updatenumj(pid);
				out.print("<script>alert('操作成功！');location.href='admin/success.jsp';</script>");
			} else {
				out.print("<script>alert('操作失败！');history.go(-1);</script>");
			}
		}else if (kind == 5) {
			int id = Integer.parseInt(request.getParameter("id"));
			int money = Integer.parseInt(request.getParameter("money"));
			String content =request.getParameter("content");
			SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String time = sd.format(new Date());
			bean.setMoney(money);
			bean.setContent(content);
			bean.setTime(time);
			int i = dao.rentdelete(id);
			if (i > 0) {
				int pid = Integer.parseInt(request.getParameter("pid"));
				GoodsDAO goodsdao = new GoodsDAO();
				goodsdao.updatenuma(pid);
				dao.icome(bean);
				out.print("<script>alert('操作成功！');location.href='admin/success.jsp';</script>");
			} else {
				out.print("<script>alert('操作失败！');history.go(-1);</script>");
			}
		}else if (kind == 6) {
			int id = Integer.parseInt(request.getParameter("id"));
			int i = dao.icomedelete(id);
			if (i > 0) {
				out.print("<script>alert('操作成功！');location.href='admin/icome_list.jsp';</script>");
			} else {
				out.print("<script>alert('操作失败！');history.go(-1);</script>");
			}
		}

	}

}
